﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GameEditor
{
    public partial class ObjectEditor : Form
    {
        private string directory;//chosen directory where all assets are held
        private string defaultDirectory;//where the openfile dialog starts

        public ObjectEditor()
        {
            InitializeComponent();
            //for testing set directory manually
            directory = @"C:\Users\Joshua\Desktop\TestDirectory\";
            //Build the treeview directory of assets
            BuildDirectoryView();
            
        }

        private void BuildDirectoryView()
        {
            DirectoryInfo dInfo = new DirectoryInfo(directory);
            DirectoryInfo[] subDir = dInfo.GetDirectories();

            int curNode = 0;

            treeView.Nodes[0].Nodes.Clear();
            treeView.Nodes[0].Text = directory;

            //a hidden folder named thumbs is ignored in case we
            //want to have a jpg thumbnail image with the xnb files
            foreach (DirectoryInfo di in subDir)
            {
                if (di.Name != "thumbs")
                    treeView.Nodes[0].Nodes.Add(di.Name);
                //
                DirectoryInfo[] folders = di.GetDirectories();

                if (folders.Length > 0)
                {
                    int i = 0;

                    foreach (DirectoryInfo fil in folders)
                    {
                        if (fil.Name != "thumbs")
                            treeView.Nodes[0].Nodes[curNode].Nodes.Add(fil.Name);

                        FileInfo[] subAssets = fil.GetFiles("*.txt");

                        foreach (FileInfo fi in subAssets)
                        {
                            treeView.Nodes[0].Nodes[curNode].Nodes[i].Nodes.Add(fi.Name);
                        }
                        i++;
                    }
                }

                FileInfo[] assets = di.GetFiles("*.txt");

                foreach (FileInfo fi in assets)
                {
                    treeView.Nodes[0].Nodes[curNode].Nodes.Add(fi.Name);
                }

                curNode++;
            }

            treeView.SelectedNode = treeView.Nodes[0];
        }


        private void treeView_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
        {
            //at label edit get the name before its changed
            //get the node that changed
            //folder or file
            //move the folder/file to same location using new node name as destination
            Directory.Move("source", "Destination");
            File.Move("source", "destination");
        }


        private void tsImportTexture_Click(object sender, EventArgs e)
        {
            //get selected node directory
            string path;

            if (treeView.SelectedNode.GetNodeCount(false) == 0)
                path = treeView.SelectedNode.Parent.FullPath;
            else
                path = treeView.SelectedNode.FullPath;

            OpenFileDialog fileDialog = new OpenFileDialog();
            if (true)
            {
                
            }
            fileDialog.Title = "Import Texture";

            fileDialog.Filter = "Texture Files (*.jpg;*.bmp;*.png;*.gif)|*.jpg;*.bmp;*.png;*.gif|" +
                                "JPG Files (*.jpg)|*.jpg|" +
                                "BMP Files (*.bmp)|*.bmp|" +
                                "PNG Files (*.png)|*.png|" +
                                "GIF Files (*.gif)|*.gif|" +
                                "All Files (*.*)|*.*";

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                //get the path of last opened file, set as default path
                int c;
                c = fileDialog.FileName.LastIndexOf("\\");
                defaultDirectory = fileDialog.FileName.Remove(c--);


                LoadTexture(fileDialog.FileName, path);
                //rebuild the treeview to display the changes made
                BuildDirectoryView();
            }
        }

        private void LoadTexture(string p, string s)
        {
            /*Cursor = Cursors.WaitCursor;

            //Create List


            //original code unloads current model, thats pointless... we want to use ALL THE TEXTURES!

            //use the provided contentbuilder
            contentBuilder.Clear();
            contentBuilder.Add(fileName, "Texture", null, "TextureProcessor");

            // Build the texture stuff
            string buildError = contentBuilder.Build();

            if (string.IsNullOrEmpty(buildError))
            {
                // If the build succeeded, use the ContentManager to
                // load the temporary .xnb file that we just created.
                //modelViewerControl.Texture = contentManager.Load<Texture2D>("Texture");
            }
            else
            {
                // If the build failed, display an error message.
                MessageBox.Show(buildError, "Error");
            }

            Cursor = Cursors.Arrow;*/
        }

        private void addFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //find current node
            //add folder
        }

    }
}
